﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WeiBeeCommon.Helpers;

namespace QunFaExpert
{
    /// <summary>
    /// Interaction logic for KeywordSetting.xaml
    /// </summary>
    public partial class KeywordSetting : Window
    {
        public KeywordSetting()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            LoadKeywords();
        }

        private void LoadKeywords()
        {
            SearchKeywords sk = new SearchHelper().Load();
            tbCity.Text = sk.City;
            tbKeywords.Text = ListToMultilineString(sk.Keywords);
        }

        private string ListToMultilineString(List<string> list)
        {
            if(list==null) return null;
            StringBuilder sb = new StringBuilder();
            foreach (var s in list)
            {
                sb.Append(s);
                if ( !string.Equals(s, list.Last()))
                {
                    sb.Append(" ");
                }
            }
            return sb.ToString();
        }

        private void btOk_Click(object sender, RoutedEventArgs e)
        {
            SearchKeywords sk = new SearchKeywords();
            sk.City = tbCity.Text;
            var slist = tbKeywords.Text.Split(new char[] { ' ', ',' },StringSplitOptions.RemoveEmptyEntries);
            sk.Keywords = slist.ToList();
            new SearchHelper().Save(sk);
            MessageBox.Show("保存成功！");
            LoadKeywords();
        }
    }
}
